//
//  Chufang_AddViewController.m
//  TY-C
//
//  Created by xyyl on 2024/4/19.
//

#import "Chufang_AddViewController.h"
#import "Chufang_addView.h"
#import "ChufangModel.h"

@interface Chufang_AddViewController ()<YBPopupMenuDelegate>
///
@property (nonatomic,strong) UILabel * titleLab;
///处方名称
@property (nonatomic,strong) UILabel * cfLab;
///
@property (nonatomic,strong) UITextField * cfTxt;
///处方类型
@property (nonatomic,strong) UILabel * topLab;
///处方类型选择框
@property (nonatomic,strong) UIButton * topBtn;

@property (nonatomic,strong) UIView * etsView;

///治疗时间
@property (nonatomic,strong) Chufang_addView * zlsjView;
///持续时间
@property (nonatomic,strong) Chufang_addView * cxsjView;
///延迟时间
@property (nonatomic,strong) Chufang_addView * ycsjView;
///频率
@property (nonatomic,strong) Chufang_addView * plView;
///
@property (nonatomic,strong) UIButton * backBtn;
///
@property (nonatomic,strong) UIButton * saveBtn;

///
@property (nonatomic,strong) NSString * typeStr;
///
@property (nonatomic,strong) UIView * nMESView;

///n治疗时间
@property (nonatomic,strong) Chufang_addView * nzlsjView;
///n持续时间
@property (nonatomic,strong) Chufang_addView * ncxsjView;
///n间隔时间
@property (nonatomic,strong) Chufang_addView * njgsjView;
///n上升时间
@property (nonatomic,strong) Chufang_addView * nsssjView;
///n下降时间
@property (nonatomic,strong) Chufang_addView * nxjsjView;
///n频率
@property (nonatomic,strong) Chufang_addView * nplView;
///n脉宽
@property (nonatomic,strong) Chufang_addView * nmkView;

@end

@implementation Chufang_AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.userInteractionEnabled = YES;
    [self initTopView];
    [self initCardViews];

    [self initBottomViews];
    
    if (_pModel) {
        [self initViewsData];
    }
    
}

- (void)initViewsData {
    _cfTxt.text = _pModel.cfmc;

    if ([_pModel.cflx isEqualToString:@"ETS"]) {
        _nMESView.hidden = YES;
        _etsView.hidden = NO;
        _typeStr = @"0";
        _plView.textField.text = _pModel.pl;
        [_plView.slider setValue:[_pModel.pl floatValue]];
        
        _zlsjView.textField.text = _pModel.zlsj;
        [_zlsjView.slider setValue:[_pModel.zlsj floatValue]];

        _cxsjView.textField.text = _pModel.cxsj;
        [_cxsjView.slider setValue:[_pModel.cxsj floatValue]];
        
        _ycsjView.textField.text = _pModel.ycsj;
        [_ycsjView.slider setValue:[_pModel.ycsj floatValue]];
        
        
    }else{
        _nMESView.hidden = NO;
        _etsView.hidden = YES;
        _typeStr = @"1";
        
        _nplView.textField.text = _pModel.pl;
        [_nplView.slider setValue:[_pModel.pl floatValue]];
        
        _nmkView.textField.text = _pModel.mk;
        [_nmkView.slider setValue:[_pModel.mk floatValue]];
        
        _nzlsjView.textField.text = _pModel.zlsj;
        [_nzlsjView.slider setValue:[_pModel.zlsj floatValue]];
        
        _ncxsjView.textField.text = _pModel.cxsj;
        [_ncxsjView.slider setValue:[_pModel.cxsj floatValue]];
        
        
        _njgsjView.textField.text = _pModel.jgsj;
        [_njgsjView.slider setValue:[_pModel.jgsj floatValue]];
        
        
        _nsssjView.textField.text = _pModel.sssj;
        [_nsssjView.slider setValue:[_pModel.sssj floatValue]];
        
        _nxjsjView.textField.text = _pModel.xjsj;
        [_nxjsjView.slider setValue:[_pModel.xjsj floatValue]];
        
    }
    NSArray * array11 = @[@" 触发电刺激 ETS",@"  神经肌肉电刺激 NMES"];
    [_topBtn setTitle:array11[[_typeStr integerValue]] forState:0];
}

- (void)initTopView {
    _titleLab = [UILabel new];
    [self.view addSubview:_titleLab];
    _titleLab.frame = CGRectMake(25 * IPADSCALE, 20 * IPADSCALE, 200, 18 * IPADSCALE);
    _titleLab.text = @"新增处方";
    _titleLab.font = [UIFont boldSystemFontOfSize:20];
    _titleLab.textColor = [UIColor blackColor];
    
    _cfLab = [UILabel new];
    [self.view addSubview:_cfLab];
    _cfLab.frame = CGRectMake(CGRectGetMinX(_titleLab.frame), CGRectGetMaxY(_titleLab.frame)+16, 45 * IPADSCALE, 27 * IPADSCALE);
    _cfLab.text = @"处方名称";
    _cfLab.font = ZYQFONT(18);
    _cfLab.textColor = RGBColor(102, 102, 102);
    
    _cfTxt = [UITextField new];
    [self.view addSubview:_cfTxt];
    _cfTxt.frame = CGRectMake(CGRectGetMaxX(_cfLab.frame), CGRectGetMaxY(_titleLab.frame)+16, 160 * IPADSCALE, 27 * IPADSCALE);
    _cfTxt.placeholder = @"请输入处方名称";
    _cfTxt.backgroundColor = [UIColor whiteColor];
    _cfTxt.textColor = [UIColor blackColor];
    _cfTxt.text = @"new处方2";
    _cfTxt.clipsToBounds = YES;
    _cfTxt.layer.borderWidth = 1;
    _cfTxt.layer.borderColor = RGBColor(176, 176, 176).CGColor;
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, CGRectGetHeight(_cfTxt.frame))];
    imageView.image = [UIImage imageNamed:@"img_none"];
    UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, CGRectGetHeight(_cfTxt.frame))];
    [leftView addSubview:imageView];
    _cfTxt.leftView = imageView;
    _cfTxt.leftViewMode = UITextFieldViewModeAlways;
    
    
    _topLab =[UILabel new];
    _topBtn = [UIButton new];
    [self.view addSubview:_topBtn];
    [self.view addSubview:_topLab];
    
    [_topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.top.equalTo(_cfTxt);
        make.right.equalTo(self.view.mas_right).offset(-40*IPADSCALE-LEFTWWW);
    }];
//    [_topBtn setBackgroundImage:[UIImage imageNamed:@"img_select"] forState:0];
    [_topBtn setTitle:@" 触发电刺激 ETS" forState:0];
    _typeStr = @"0";
    [_topBtn setBackgroundColor:[UIColor whiteColor]];
    _topBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    _topBtn.titleLabel.font = ZYQFONT(20);
    [_topBtn setTitleColor:RGBColor(51, 51, 51) forState:0];
    [_topBtn addTarget:self action:@selector(topBtnAction) forControlEvents:UIControlEventTouchUpInside];
    _topBtn.clipsToBounds = YES;
    _topBtn.layer.borderWidth = 1;
    _topBtn.layer.borderColor = RGBColor(176, 176, 176).CGColor;
    
    
    
    [_topLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.equalTo(self->_cfLab);
        make.right.equalTo(self->_topBtn.mas_left).offset(-5*IPADSCALE);
    }];
    _topLab.text = @"处方类型";
    _topLab.font = ZYQFONT(20);
    _topLab.textColor = RGBColor(102, 102, 102);


}

- (void)initCardViews {
    _etsView = [UIView new];
    [self.view addSubview:_etsView];
    _etsView.frame = CGRectMake(CGRectGetMinX(_cfLab.frame), CGRectGetMaxY(_cfLab.frame)+10*IPADSCALE, ZYQWIDTH - (37+40)*IPADSCALE - LEFTWWW, 27*IPADSCALE * 4 + 20 *IPADSCALE * 3);

    
//    _zlsjView = [[Chufang_addView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_cfLab.frame), CGRectGetMaxY(_cfLab.frame)+10*IPADSCALE, ZYQWIDTH - (37+40)*IPADSCALE - LEFTWWW, 27*IPADSCALE)];
    _zlsjView = [[Chufang_addView alloc]initWithFrame:CGRectMake(0, 0, ZYQWIDTH - (37+40)*IPADSCALE - LEFTWWW, 27*IPADSCALE)];

    _zlsjView.nameLab.text = @"治疗时间";
    _zlsjView.slider.maximumValue = 60;
    _zlsjView.slider.minimumValue = 1;
    _zlsjView.minLab.text = @"1min";
    _zlsjView.maxLab.text = @"60min";
    _zlsjView.type = 0;

    [_zlsjView.slider setValue:30];

    [_etsView addSubview:_zlsjView];
    
    _cxsjView = [[Chufang_addView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_zlsjView.frame), CGRectGetMaxY(_zlsjView.frame)+20*IPADSCALE, CGRectGetWidth(_zlsjView.frame), CGRectGetHeight(_zlsjView.frame))];
    _cxsjView.nameLab.text = @"持续时间";
    _cxsjView.slider.maximumValue = 4;
    _cxsjView.slider.minimumValue = 0;
    _cxsjView.minLab.text = @"0s";
    _cxsjView.maxLab.text = @"4s";
    _cxsjView.type = 0;
    [_cxsjView.slider setValue:2];
    [_etsView addSubview:_cxsjView];
    
    _ycsjView = [[Chufang_addView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_zlsjView.frame), CGRectGetMaxY(_cxsjView.frame)+20*IPADSCALE, CGRectGetWidth(_zlsjView.frame), CGRectGetHeight(_zlsjView.frame))];
    _ycsjView.nameLab.text = @"延迟时间";
    _ycsjView.slider.maximumValue = 4;
    _ycsjView.slider.minimumValue = 0.1;
    _ycsjView.minLab.text = @"0.1s";
    _ycsjView.maxLab.text = @"4s";
    _ycsjView.type = 1;
    [_ycsjView.slider setValue:2];

    [_etsView addSubview:_ycsjView];
    
    _plView = [[Chufang_addView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_zlsjView.frame), CGRectGetMaxY(_ycsjView.frame)+20*IPADSCALE, CGRectGetWidth(_zlsjView.frame), CGRectGetHeight(_zlsjView.frame))];
    _plView.nameLab.text = @"频率";
    _plView.slider.maximumValue = 100;
    _plView.slider.minimumValue = 2;
    _plView.minLab.text = @"2Hz";
    _plView.maxLab.text = @"100Hz";
    _plView.type = 0;

    [_plView.slider setValue:51];

    [_etsView addSubview:_plView];
    
    
//    _etsView.hidden = YES;
    ////
    _nMESView = [UIView new];
    [self.view addSubview:_nMESView];
    _nMESView.hidden = YES;

    _nMESView.frame = CGRectMake(CGRectGetMinX(_cfLab.frame), CGRectGetMaxY(_cfLab.frame)+10*IPADSCALE, ZYQWIDTH - (37+40)*IPADSCALE - LEFTWWW, 27*IPADSCALE * 7 + 10 *IPADSCALE * 6);
    
    
    _nzlsjView = [[Chufang_addView alloc]initWithFrame:CGRectMake(0, 0, ZYQWIDTH - (37+40)*IPADSCALE - LEFTWWW, 27*IPADSCALE)];

    _nzlsjView.nameLab.text = @"治疗时间";
    _nzlsjView.slider.maximumValue = 120;
    _nzlsjView.slider.minimumValue = 1;
    _nzlsjView.minLab.text = @"1min";
    _nzlsjView.maxLab.text = @"120min";
    _nzlsjView.type = 0;
    [_nzlsjView.slider setValue:30];
    [_nMESView addSubview:_nzlsjView];
    
    _ncxsjView = [[Chufang_addView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_nzlsjView.frame), CGRectGetMaxY(_nzlsjView.frame)+10*IPADSCALE, CGRectGetWidth(_zlsjView.frame), CGRectGetHeight(_zlsjView.frame))];
    _ncxsjView.nameLab.text = @"持续时间";
    _ncxsjView.slider.maximumValue = 100;
    _ncxsjView.slider.minimumValue = 1;
    _ncxsjView.minLab.text = @"1s";
    _ncxsjView.maxLab.text = @"100";
    _ncxsjView.type = 0;
    [_ncxsjView.slider setValue:2];
    [_nMESView addSubview:_ncxsjView];
    
    _njgsjView = [[Chufang_addView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_nzlsjView.frame), CGRectGetMaxY(_ncxsjView.frame)+10*IPADSCALE, CGRectGetWidth(_zlsjView.frame), CGRectGetHeight(_zlsjView.frame))];
    _njgsjView.nameLab.text = @"间隔时间";
    _njgsjView.slider.maximumValue = 20;
    _njgsjView.slider.minimumValue = 1;
    _njgsjView.minLab.text = @"1s";
    _njgsjView.maxLab.text = @"20s";
    _njgsjView.type = 0;
    [_njgsjView.slider setValue:2];
    [_nMESView addSubview:_njgsjView];
    
    
    _nsssjView = [[Chufang_addView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_nzlsjView.frame), CGRectGetMaxY(_njgsjView.frame)+10*IPADSCALE, CGRectGetWidth(_zlsjView.frame), CGRectGetHeight(_zlsjView.frame))];
    _nsssjView.nameLab.text = @"上升时间";
    _nsssjView.slider.maximumValue = 100;
    _nsssjView.slider.minimumValue = 0;
    _nsssjView.minLab.text = @"0s";
    _nsssjView.maxLab.text = @"100s";
    _nsssjView.type = 0;
    [_nsssjView.slider setValue:20];
    [_nMESView addSubview:_nsssjView];
    
    _nxjsjView = [[Chufang_addView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_nzlsjView.frame), CGRectGetMaxY(_nsssjView.frame)+10*IPADSCALE, CGRectGetWidth(_zlsjView.frame), CGRectGetHeight(_zlsjView.frame))];
    _nxjsjView.nameLab.text = @"下降时间";
    _nxjsjView.slider.maximumValue = 100;
    _nxjsjView.slider.minimumValue = 0;
    _nxjsjView.minLab.text = @"0s";
    _nxjsjView.maxLab.text = @"100s";
    _nxjsjView.type = 0;
    [_nxjsjView.slider setValue:20];
    [_nMESView addSubview:_nxjsjView];
    
    _nplView = [[Chufang_addView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_nzlsjView.frame), CGRectGetMaxY(_nxjsjView.frame)+10*IPADSCALE, CGRectGetWidth(_zlsjView.frame), CGRectGetHeight(_zlsjView.frame))];
    _nplView.nameLab.text = @"频率";
    _nplView.slider.maximumValue = 100;
    _nplView.slider.minimumValue = 2;
    _nplView.minLab.text = @"2Hz";
    _nplView.maxLab.text = @"100Hz";
    _nplView.type = 0;
    [_nplView.slider setValue:20];
    [_nMESView addSubview:_nplView];
    
    _nmkView = [[Chufang_addView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_nzlsjView.frame), CGRectGetMaxY(_nplView.frame)+10*IPADSCALE, CGRectGetWidth(_zlsjView.frame), CGRectGetHeight(_zlsjView.frame))];
    _nmkView.nameLab.text = @"脉宽";
    _nmkView.slider.maximumValue = 450;
    _nmkView.slider.minimumValue = 50;
    _nmkView.minLab.text = @"50μs";
    _nmkView.maxLab.text = @"450μs";
    _nmkView.type = 0;
    [_nmkView.slider setValue:100];
    [_nMESView addSubview:_nmkView];
    
    
}

- (void)topBtnAction {
    [YBPopupMenu showRelyOnView:_topBtn titles:@[@" 触发电刺激 ETS",@"  神经肌肉电刺激 NMES"] icons:nil menuWidth:CGRectGetWidth(_topBtn.frame) otherSettings:^(YBPopupMenu *popupMenu) {
        popupMenu.delegate = self;
        popupMenu.tag = 3002;
        popupMenu.font = ZYQFONT(20);
    }];
    
}

- (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index {
    NSArray * array11 = @[@" 触发电刺激 ETS",@"  神经肌肉电刺激 NMES"];
    _typeStr = [NSString stringWithFormat:@"%ld",index];
    [_topBtn setTitle:array11[index] forState:0];
    if ([_typeStr isEqualToString:@"0"]) {
        _etsView.hidden = NO;
        _nMESView.hidden = YES;
    }else{
        _etsView.hidden = YES;
        _nMESView.hidden = NO;
    }
}

- (void)initBottomViews {
    
    _backBtn = [UIButton new];
    [self.view addSubview:_backBtn];
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(80*IPADSCALE));
        make.height.equalTo(@(30*IPADSCALE));
        make.bottom.equalTo(self.view.mas_bottom).offset(-15*IPADSCALE);
        make.right.equalTo(self.view.mas_centerX).offset(-50*IPADSCALE);
    }];
    [_backBtn setTitle:@"返回" forState:0];
    [_backBtn setTitleColor:RGBColor(13, 157, 219) forState:0];
    _backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:30];
    _backBtn.clipsToBounds = YES;
    _backBtn.layer.borderWidth = 1;
    _backBtn.layer.cornerRadius = 15*IPADSCALE;
    _backBtn.layer.borderColor = RGBColor(13, 157, 219).CGColor;
    
    _saveBtn = [UIButton new];
    [self.view addSubview:_saveBtn];
    [_saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.height.width.equalTo(_backBtn);
        make.left.equalTo(_backBtn.mas_right).offset(30*IPADSCALE);
    }];
    [_saveBtn setTitle:@"保存" forState:0];
    [_saveBtn setTitleColor:[UIColor whiteColor] forState:0];
    _saveBtn.titleLabel.font = [UIFont boldSystemFontOfSize:30];
    _saveBtn.clipsToBounds = YES;
    _saveBtn.layer.borderWidth = 1;
    _saveBtn.layer.cornerRadius = 15*IPADSCALE;
    _saveBtn.layer.borderColor = RGBColor(13, 157, 219).CGColor;
    [_saveBtn setBackgroundColor:RGBColor(13, 157, 219)];
    
    
    _saveBtn.tag = 1;
    _backBtn.tag = 0;
    
    [_saveBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)backBtnAction:(UIButton *)btn {
    if(btn.tag == 1){
        ///保存操作
        
        ChufangModel * pp = [[ChufangModel alloc]init];
        pp.isfscs = @"1";
        pp.isbj = @"1";
        pp.isdel = @"1";
        pp.cfmc = _cfTxt.text;
        pp.uploadTime = [NSString getNowTimeStr];

        
        if ([_typeStr isEqualToString:@"0"]) {
            pp.cflx = @"ETS";
            pp.pl = _plView.textField.text;
            pp.mk = @"";
            pp.zlsj =_zlsjView.textField.text;
            pp.cxsj =_cxsjView.textField.text;
            pp.jgsj = @"";
            pp.sssj = @"";
            pp.xjsj = @"";
            pp.ycsj = _ycsjView.textField.text;
            
        }else{
            pp.cflx = @"NMES";
            pp.pl = _nplView.textField.text;
            pp.mk = _nmkView.textField.text;
            pp.zlsj = _nzlsjView.textField.text;
            pp.cxsj = self.ncxsjView.textField.text;
            pp.jgsj = _njgsjView.textField.text;
            pp.sssj = _nsssjView.textField.text;
            pp.xjsj = _nxjsjView.textField.text;
            pp.ycsj = @"";
        }
        
        if (_pModel) {
            pp.creatTime = _pModel.creatTime;
            [TY_DataCommand updateDataWithChufangModel:pp];

        }else{
            pp.creatTime = [NSString getNowTimeStr];
            [TY_DataCommand addDataByPeopleModel:pp];

        }
        

        [[NSNotificationCenter defaultCenter] postNotificationName:@"chufangRefresh" object:nil];
        
    }
    [self.view removeFromSuperview];///移除view
    [self removeFromParentViewController];///移除父控制器
}


@end
